//
//  AccountManageViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/19.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "AccountManageViewController.h"
#import "UserCarViewController.h"
#import "UserCode.h"

@interface AccountManageViewController ()<UISearchBarDelegate>
{
    NSMutableArray *_allDataArray;
}
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation AccountManageViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getUserList];
}
-(void)getUserList
{
    NSString *URLString = @"/axq/admin/users";
    NSDictionary *parameters = @{@"peopletel": @""};
    [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:parameters RequestSuccess:^(NSURLResponse *response, id responseObject) {
        NSLog(@"%@",responseObject);
        self.tableView.className=@"PropertyCell";
        NSMutableArray *itemArr=[NSMutableArray array];
        NSString *code=[NetworkPath getValue:@"code" from:responseObject];
        if (code.length) {
            if([code isEqualToString:@"10008"])
            {
                if ([NetworkPath isNSArray:@"data" from:responseObject])
                {
                    NSArray *data=responseObject[@"data"];
                    for (int i=0; i<data.count; i++) {
                        if ([data[i] isKindOfClass:[NSDictionary class]]) {
                            NSDictionary *dic=data[i];
                            UserCode *model=[[UserCode alloc]init];
                            model.someid=[NSString stringWithFormat:@"%@",dic[@"id"]];
                            model.peoplename=[NSString stringWithFormat:@"%@",dic[@"peoplename"]];
                            model.peopletel=[NSString stringWithFormat:@"%@",dic[@"peopletel"]];
                            model.headimg=[NSString stringWithFormat:@"%@",dic[@"headimg"]];
                            
                            propertyData *dataObj=[[propertyData alloc]init];
                            dataObj.image=model.headimg;
                            dataObj.name=model.peoplename;
                            dataObj.phone=model.peopletel;
                            dataObj.someid=model.someid;
                            [itemArr addObject:dataObj];
                            
                            
                            
                            
                        }
                    }
                    
                }
            }else
            {
                NSString *str=[NetworkPath getValue:@"msg" from:responseObject];
                
                [self.view makeToast:str];
            }
        }
        _allDataArray=itemArr;
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView reloadData];
    } RequestFailed:^(NSError *error) {
        self.tableView.className=@"PropertyCell";
        NSMutableArray *itemArr=[NSMutableArray array];
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _allDataArray=[NSMutableArray array];
    
    self.view.backgroundColor=[UIColor colorF];
    self.navigationController.navigationBar.barTintColor = [UIColor colorQ];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    
    
    
    self.navigationItem.title = @"用户管理";
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    
    
    
    
    UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    searchBar.keyboardType=UIKeyboardTypeNumberPad;
    searchBar.delegate=self;
    searchBar.placeholder=@"搜索手机号";
    searchBar.backgroundImage=[NetworkPath createImageWithColor:[UIColor colorF]];
    
    [[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
    UITextField *txfSearchField = [searchBar valueForKey:@"_searchField"];
    [txfSearchField setDefaultTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.5]}];
    
    searchBar.searchTextPositionAdjustment = UIOffsetMake(7, 0);
    
    
    
    self.tableView=[SingLeton myTableViewWithFrame:CGRectMake(0, TOP_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT) withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        PropertyCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        UserCarViewController *vc=[[UserCarViewController alloc]init];
        vc.peopleid=cell.dataDic.someid;
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView=searchBar;
    
    
}

//调用方法
- (UIImage*)searchFieldBackgroundImage {
    UIColor*color = [UIColor whiteColor];
    CGFloat cornerRadius = 5;
    CGRect rect =CGRectMake(0,0,28,28);
    
    UIBezierPath*roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    roundedRect.lineWidth=0;
    
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    [color setFill];
    [roundedRect fill];
    [roundedRect stroke];
    [roundedRect addClip];
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
//编辑文字改变的回调
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    
    
    
    if (_allDataArray.count) {
        
        
        self.tableView.className=@"PropertyCell";
        NSMutableArray *itemArr=[NSMutableArray array];
        if (searchBar.text.length) {
            for (int i=0; i<_allDataArray.count; i++) {
                propertyData *obj=_allDataArray[i];
                if ([obj.phone rangeOfString:searchBar.text].location !=NSNotFound) {
                    [itemArr addObject:obj];
                }
            }
        }else
        {
            itemArr=_allDataArray;
        }
        
        
        
        
        [self.tableView.dataArray addObject:itemArr];
        [self.tableView reloadData];
        
        
    }
    
    
    
    
    
    
}
@end
